package com.heima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.mapper.KsClassMapper;
import com.heima.mapper.UserMapper;
import com.heima.pojos.KsClass;
import com.heima.pojos.User;
import com.heima.pojos.vo.UserVo;
import com.heima.service.KsClassService;
import com.heima.service.UserService;
import com.heima.utils.UserThreadLocal;
import com.heima.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private KsClassService ksClassService;
    @Autowired
    private KsClassMapper ksClassMapper;

    /**
     * 根据账号判断是否是老师
     *
     * @param mobile
     * @return
     */
    @Override
    public R selectTeacher(String mobile) {
        /*LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getMobile, mobile);
        User user = userMapper.selectById(mobile);*/
//        System.out.println(mobile);
        User one = this.getOne(Wrappers.lambdaQuery(User.class).eq(User::getMobile, mobile));
        int classId = one.getClassId();
        //老师登录
        if (one.getRoleId() == 1) {
            List<KsClass> list = ksClassService.list();
            return R.OK(list);
        }
        //学生登录
        if (one.getRoleId() == 2) {
            List<KsClass> classes = new ArrayList<>();
            KsClass ksClass = ksClassMapper.selectById(classId);
            classes.add(ksClass);

            return R.OK(classes);
        }
        return R.success("ok");
    }

    /**
     * 登录
     *
     * @param user
     * @return
     */
    @Override
    public R login(User user) {
        //判断用户名密码是否为空
        if (!StrUtil.isAllNotBlank(user.getMobile(), user.getPassword())) {
            return R.error("请输入用户名密码");
        }
        ;

        //判断该用户是否存在
        User one = this.getOne(Wrappers.lambdaQuery(User.class).eq(User::getMobile, user.getMobile()));
        if (ObjectUtil.isNull(one)) {
            return R.error("改用户不存在");
        }
        ;

        String password = user.getPassword();

        //进行密码比对
        if (!StrUtil.equals(password, one.getPassword(), true)) {
            return R.error("密码错误");
        }
        ;

        //判断通过
        return R.OK(one);

    }

    /**
     * 查询全部
     *
     * @return
     */
    @Override
    public R selectAll() {
        List<UserVo> userVos = new ArrayList<>();
        List<User> users = userMapper.selectList(null);

        Map<Long, Integer> collect = users.stream().collect(Collectors.groupingBy(User::getGroupId, Collectors.summingInt(User::getIntegral)));

        for (User user : users) {
            UserVo userVo = new UserVo();
            BeanUtil.copyProperties(user, userVo);
            userVo.setGroupsIntegral(collect.get(Convert.toLong(userVo.getGroupId())));
            userVos.add(userVo);
        }

        return R.OK(userVos);
    }

    /**
     * 修改积分
     *
     * @param user
     * @return
     */
    @Override
    public R updateintegral(User user) {
        User one = this.getOne(Wrappers.lambdaQuery(User.class).eq(User::getId, user.getId()));
        if (one != null) {
            int integral = one.getIntegral();
            one.setIntegral(integral + user.getIntegral());
            userMapper.updateById(one);
        }
        return R.OK("");
    }

    /**
     * 重置全班积分
     *
     * @param classId
     * @return
     */
    //todo
    @Override
    public R re(Long[] classId) {
        int classId1 = UserThreadLocal.get().getClassId();
        LambdaUpdateWrapper<User> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(User::getClassId, classId1).set(User::getIntegral, 0);
        userMapper.update(null, lambdaUpdateWrapper);
        return R.OK("");
    }
}
